package com.zt.rongda.reimburse.controller;

import com.zt.rongda.constant.BusConstant;
import com.zt.servlet.*;
import com.zt.rongda.reimburse.model.ReimburseBill;
import com.zt.rongda.reimburse.service.ReimburseBillService;
import com.zt.system.model.Role;
import com.zt.system.service.RoleService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/reimburseBill")
public class ReimburseBillController {

    @Autowired
    private ReimburseBillService reimburseBillService;

    @Autowired
    private RoleService roleService;

    private List<String> getRoleIdListByUser(String userId){
        List<String> roleIds = null;
        List<Role> roles = roleService.queryRoleOfUser(userId);

        if(roles != null && !roles.isEmpty()){
            roleIds = new ArrayList<String>();
            for(Role role : roles){
                String roleId = role.getId();
                roleIds.add(roleId);
            }
        }
        return roleIds;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(reimburseBillService.getTable(reqBean));
    }


    /**
     * 查询审批任务的报销单数据
     * @param reqBean
     * @return
     */
//    @RequestMapping(value = "/query4Audit")
//    @ResponseBody
//    public JSONObject query4Audit(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
//        return Response.response(reimburseBillService.query4Audit(reqBean.getInfo("reimburseId")));
//    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        reimburseBillService.add(getReimburseBill(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        reimburseBillService.update(getReimburseBill(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/apply")
    @ResponseBody
    public JSONObject apply(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        reimburseBillService.apply(reqBean.getInfo("reimburseId"));
        return Response.responseSuccess();
    }

//    @RequestMapping(value = "/reimburseCheck")
//    @ResponseBody
//    public JSONObject reimburseCheck(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
//        reimburseBillService.reimburseCheck(reqBean.getInfo("reimburseId"), reqBean.getInfo("remark"));
//        return Response.responseSuccess();
//    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        reimburseBillService.delete(reqBean.getInfo("reimburseId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/tasks")
    @ResponseBody
    public JSONObject tasks(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        if(!BusConstant.SUPER_USER_ID.equals(reqBean.getUserId())){
            reqBean.addInfo("candidateOrAssignedUserId", reqBean.getUserId());
            reqBean.addInfo("candidateGroups", getRoleIdListByUser(reqBean.getUserId()));
        }
        return Response.response(reimburseBillService.getTasks(reqBean));
    }


    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private ReimburseBill getReimburseBill(ReqBean reqBean) {
        ReimburseBill reimburseBill = new ReimburseBill();
        reimburseBill.setReimburseId(reqBean.getInfo("reimburseId"));
        reimburseBill.setOrderId(reqBean.getInfo("orderId"));
        reimburseBill.setReimburseUsage(reqBean.getInfo("reimburseUsage"));
        reimburseBill.setReimburseType(reqBean.getInfo("reimburseType"));
        reimburseBill.setReimburseCount(reqBean.getInfo("reimburseCount"));
        reimburseBill.setTotalAmount(reqBean.getInfoDouble("totalAmount"));
        reimburseBill.setDeptId(reqBean.getInfo("deptId"));
        reimburseBill.setEmpId(reqBean.getInfo("empId"));
        reimburseBill.setStatus(reqBean.getInfo("status"));
        reimburseBill.setOperator(reqBean.getUserId());
        reimburseBill.setRemark(reqBean.getInfo("remark"));
        return reimburseBill;
    }

}